package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.api.cache.domain.TravelStatisVO;
import cn.wolfcode.luowowo.api.cache.prefix.BasekeyPrefix;
import cn.wolfcode.luowowo.api.cache.prefix.TravelStatisKeyPrefix;
import cn.wolfcode.luowowo.api.cache.service.IRedisService;
import cn.wolfcode.luowowo.api.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.ITravelService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class TravelStatisRedisServiceImpl implements ITravelStatisRedisService {
    //定义常量
    public static final int VO_NUM_TYPE_VIEW = 0;//阅读
    public static final int VO_NUM_TYPE_REPLY= 1;//评论
    public static final int VO_NUM_TYPE_FAVOR= 2;//收藏
    public static final int VO_NUM_TYPE_SHARE = 3;//分享
    public static final int VO_NUM_TYPE_THUMBSUP = 4;//点赞
    @Autowired
    private IRedisService redisService;
    @Reference
    private ITravelService travelService;
    public void viewIncrease(TravelStatisKeyPrefix keyPrefix, Long sid) {
        numIncrease(keyPrefix,sid,VO_NUM_TYPE_VIEW);
    }

    public void setStatisVo(TravelStatisVO vo) {

        redisService.set(TravelStatisKeyPrefix.TRAVEL_STATIS_VO,vo.getTravelId()+"",vo);
    }

    public TravelStatisVO getStatisVo(Long sid) {
        if(!redisService.exists(TravelStatisKeyPrefix.TRAVEL_STATIS_VO,sid+"")){
            return null;
        }

        return redisService.get(TravelStatisKeyPrefix.TRAVEL_STATIS_VO,sid+"",TravelStatisVO.class);
    }

    public void replyIncrease(TravelStatisKeyPrefix keyPrefix, Long sid) {
        numIncrease(keyPrefix,sid,VO_NUM_TYPE_REPLY);
    }


    public boolean favor(TravelStatisKeyPrefix keyPrefix, Long sid, Long uid) {
        ArrayList<Object> list = new ArrayList<>();
        //获取数据对象
        TravelStatisVO vo = this.getStatisVo(sid);
        //判断收藏是否存在redis中
        boolean flag = false;
        if(!redisService.exists(keyPrefix,sid + "")){
            //不存在
            list.add(uid +"");
            //收藏数加一
            vo.setFavornum(vo.getFavornum() + 1);
            flag = true;
        }else{
            //存在
           list = redisService.get(keyPrefix, sid + "", ArrayList.class);
           //判断list中是否包含响应的uid
            if(list.contains(uid + "")){
                //包含,移除uid,并且收藏数量-1
                list.remove(uid + "");
                vo.setFavornum(vo.getFavornum() - 1);
                flag = false;
            }else{
                //不包含,添加uid,并且收藏数量+1
                list.add(uid+"");
                vo.setFavornum(vo.getFavornum()+1);
                flag = true;
            }
        }
        //统一保存vo,和list
        this.setStatisVo(vo);
        redisService.set(keyPrefix,sid+"",list);
        return flag;

    }





    public boolean thumbup(TravelStatisKeyPrefix keyPrefix, Long sid, Long uid) {
        //此处的key格式为 travel_statis_thumbsup : sid : uid
        String key = sid + ":" + uid;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        //获取数据对象
        TravelStatisVO vo = this.getStatisVo(sid);
        if(!redisService.exists(keyPrefix,key)){
            //key不存在,可以顶,创建点赞对象
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            redisService.set(keyPrefix, key,today);
            this.setStatisVo(vo);
            return true;

        }else{
            //key 存在
            //从redis中取出点赞对象
            String value = redisService.get(keyPrefix, key, String.class);
            //如果不是今天,那么可以点赞,并且将value值设置进去
            if(!today.equals(value)){
                vo.setThumbsupnum(vo.getThumbsupnum() + 1);
                redisService.set(keyPrefix,key,today);
                this.setStatisVo(vo);
                return true;
            }
        }

        return false;
    }

    public List<TravelStatisVO> getAllStatisVos() {
        //要获取数据对象,必须先拿到所有的keys
        Set keys = redisService.getKeysByPattern(TravelStatisKeyPrefix.TRAVEL_STATIS_VO.getPrefix() + ":*");
        ArrayList<TravelStatisVO> list = new ArrayList<>();
        //遍历keys获取所有的数据对象,并存入到list中
        for (Object key : keys) {
           list.add(redisService.getByRealKey(key+"",TravelStatisVO.class));
        }
        return list;
    }
    //站在用户角度,收藏
    public boolean Userfavor(TravelStatisKeyPrefix keyPrefix, Long sid, Long uid) {
        ArrayList<Object> list = new ArrayList<>();
        //获取数据对象
        TravelStatisVO vo = this.getStatisVo(sid);
        //判断点赞是否存在redis中
        boolean flag = false;
        if(!redisService.exists(keyPrefix,uid + "")){
            //不存在
            list.add(sid +"");
            //点赞数量增加1
            vo.setFavornum(vo.getFavornum() + 1);
            flag = true;
        }else{
            //存在
            list = redisService.get(keyPrefix, uid + "", ArrayList.class);
            //判断list中是否包含响应的uid
            if(list.contains(sid + "")){
                //包含,移除uid,并且收藏数量-1
                list.remove(sid + "");
                vo.setFavornum(vo.getFavornum() - 1);
                flag = false;
            }else{
                //不包含,添加uid,并且收藏数量+1
                list.add(sid+"");
                vo.setFavornum(vo.getFavornum()+1);
                flag = true;
            }
        }
        //统一保存vo,和list
        this.setStatisVo(vo);
        redisService.set(keyPrefix,uid+"",list);
        return flag;
    }


    public boolean thumbupTime(TravelStatisKeyPrefix keyPrefix, Long sid, Long uid) {
        String key = sid + ":" + uid;
        //获取数据对象
        TravelStatisVO vo = this.getStatisVo(sid);

        if(!redisService.exists(keyPrefix,key)){
            //key不存在,可以顶,创建点赞对象
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            redisService.set(keyPrefix, key,"已顶过");
            this.setStatisVo(vo);
            return true;

        }

        return false;
    }

    //每点一次赞,就增加一分
    public void addScore(TravelStatisKeyPrefix keyPrefix, Long sid,int score) {
        String key = "";
        String value = TravelStatisKeyPrefix.TRAVEL_STATIS_VO.getPrefix() + ":" + sid;
        redisService.addScore(keyPrefix,key,value,score);
    }

    public List<TravelStatisVO> getAllTravelCommends() {
      List<TravelStatisVO> list =  redisService.reversRange(TravelStatisKeyPrefix.TRAVEL_STATIS_COMMEND_SORT,"",0,-1,TravelStatisVO.class);
        return list;
    }

    public List<String> queryFavorByUserId(BasekeyPrefix Prefix, Long id) {
        String s = redisService.get(Prefix, id + "");
        List<String> list = JSON.parseObject(s, ArrayList.class);
        return list;
    }


    public void numIncrease(TravelStatisKeyPrefix keyPrefix,Long sid,int type){
        TravelStatisVO vo = null;
        //判断数据对象vo对应的key值是否存在
        if(redisService.exists(keyPrefix,sid + "")){
            //如果存在,获取到vo
            vo = redisService.get(keyPrefix, sid + "", TravelStatisVO.class);
        }else{
            //如果不存在,创建vo对象,并对阅读量加1,存入redis数据库中
            vo = new TravelStatisVO();
            //根据sid查出travelDetail
            Travel st = travelService.getById(sid);
            vo.setDestId(st.getDest().getId());
            vo.setDestName(st.getDest().getName());
            vo.setTitle(st.getTitle());
            vo.setTravelId(sid);
            vo.setViewnum(st.getViewnum());
            vo.setReplynum(st.getReplynum());
            vo.setFavornum(st.getFavornum());
            vo.setSharenum(st.getSharenum());
            vo.setThumbsupnum(st.getThumbsupnum());

        }

        //统一加数据
        switch (type){
            case VO_NUM_TYPE_VIEW :
            vo.setViewnum(vo.getViewnum() + 1);
            break;
            case VO_NUM_TYPE_REPLY :
            vo.setReplynum(vo.getReplynum()+1);
            break;
            case VO_NUM_TYPE_FAVOR :
            vo.setFavornum(vo.getFavornum() + 1);
            break;
            case VO_NUM_TYPE_SHARE :
            vo.setSharenum(vo.getSharenum() + 1);
            break;
            case VO_NUM_TYPE_THUMBSUP :
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            break;
        }
        //统一将vo设置进redis中
        this.setStatisVo(vo);
    }


}
